package com.sky.controller.user;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.vo.CategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("userCategoryController")
@RequestMapping("/user/category")
@Tag(name = "分类相关接口")
public class CategoryController {

    // 改为final修饰（可选但推荐，保证依赖不可变）
    private final CategoryService categoryService;

    // 构造器注入（Spring 4.3+ 单构造器时可省略@Autowired）
    @Autowired
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 分页查询获取所有的分类
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询获取所有的分类")
    public Result<PageResult> list(CategoryPageDTO categoryPageDTO) {
        log.info("分页查询获取所有的分类：{}", categoryPageDTO);
        PageResult pageResult =  categoryService.list(categoryPageDTO);
        return Result.success(pageResult);
    }



    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @Operation(summary = "根据id查询分类")
    public Result<CategoryVO> getById(Long id) {
        log.info("根据id查询分类：{}", id);
        CategoryVO categoryVO = categoryService.getById(id);
        return Result.success(categoryVO);
    }



    /**
     * 统计分类数量
     */
    @GetMapping("/count")
    @Operation(summary = "统计分类数量")
    public Result<Long> count() {
        log.info("统计分类数量");
        Long count = categoryService.countCategory();
        return Result.success(count);
    }


}
